package test;

import bean.Users;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        //java-json
        Users users = new Users(11,"aaa","123456",123.456);
        JSONObject jsonObject = JSONObject.fromObject(users);
        System.out.println(jsonObject);

        //json-java
        String str = "{\"money\":123.456,\"password\":\"123456\",\"uid\":11,\"username\":\"aaa\"}";
        JSONObject jsonObject1 = JSONObject.fromObject(str);
        Object o = JSONObject.toBean(jsonObject1, Users.class);
        System.out.println("o="+o);

        //java集合-json
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Users(1,"张三","123",12.34));
        arrayList.add(new Users(1,"张三2","123",12.34));
        arrayList.add(new Users(1,"张三3","123",12.34));
        JSONArray jsonArray = JSONArray.fromObject(arrayList);
        System.out.println(jsonArray);
        //json-java集合
        String str2 = "[{\"money\":12.34,\"password\":\"123\",\"uid\":1,\"username\":\"张三\"},{\"money\":12.34,\"password\":\"123\",\"uid\":1,\"username\":\"张三2\"},{\"money\":12.34,\"password\":\"123\",\"uid\":1,\"username\":\"张三3\"}]\n";
        JSONArray jsonArray2 = JSONArray.fromObject(str2);
        Object[] o1 = (Object[]) JSONArray.toArray(jsonArray2);
        for (Object o2 : o1) {
            System.out.println("o2="+o2);
        }
    }
}
